package com.hefeng.wx.service.Impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefeng.wx.domain.WxDeviceReport;
import com.hefeng.wx.mapper.WxDeviceReportMapper;
import com.hefeng.wx.service.WxDeviceReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**设备报警service
 * @description: TODO
 * @author：  hzh
 * @datetime：  2022/10/14 17:18
 * @参数说明
 */
@Service
public class WxDeviceReportServiceImpl extends ServiceImpl<WxDeviceReportMapper, WxDeviceReport> implements WxDeviceReportService {
    @Autowired
    public  WxDeviceReportMapper wxDeviceReportMapper;
    @Override
    public List<WxDeviceReport> getReportList(String userId) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<WxDeviceReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxDeviceReport::getUserId,userId);
        queryWrapper.ge( WxDeviceReport::getReportTime, DateUtil.offsetWeek(new Date(), -1));
        queryWrapper.orderByDesc(WxDeviceReport::getReportTime);
        List<WxDeviceReport> list=wxDeviceReportMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Page<WxDeviceReport> paginQuery(String userId, long current, long size) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<WxDeviceReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WxDeviceReport::getUserId,userId);
        queryWrapper.orderByDesc(WxDeviceReport::getReportTime);
        //2. 执行分页查询
        Page<WxDeviceReport> pagin = new Page<>(current , size);
        Page<WxDeviceReport> selectResult = this.page(pagin,queryWrapper);
        //3. 返回结果
        return pagin;
    }

    @Override
    public int insert(WxDeviceReport wxDeviceReport) {
        wxDeviceReport.setReportTime(new Date());
        wxDeviceReportMapper.insert(wxDeviceReport);
        System.out.println((""+wxDeviceReport.getReportTime()+""));

        return 0;
    }


}
